﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WPF
{
    /// <summary>
    /// 视图打开的处理队列
    /// </summary>
    public class ViewQueue : IEnumerable<ViewQueueNode>, INotifyCollectionChanged
    {
        private ObservableCollection<ViewQueueNode> Items { get; set; }

        public int Count
        {
            get
            { 
                return Items.Count;
            }
        }

        public ViewQueue()
        {
            Items = new ObservableCollection<ViewQueueNode>();
            Items.CollectionChanged += Items_CollectionChanged;
        }

        void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }

        public bool HasNode()
        {
            if (Items.Count > 0)
            {
                return true;
            }
            return false;
        }

        public void Enqueue(ViewActionBehavior behavior, View view, SwitchEffect switchEffect)
        {
            lock (this)
            {
                Items.Add(new ViewQueueNode(behavior, view, switchEffect));
            }
        }

        public ViewQueueNode GetNextRemoveCurrent()
        {
            lock (this)
            {
                if (Items.Count > 0)
                {
                    Items.RemoveAt(0);
                    if (Items.Count > 0)
                    {
                        return Items.First();
                    }
                }
                return null;
            }
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public IEnumerator<ViewQueueNode> GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }
    }
}
